local tuqiong = fk.CreateSkill{
  name = "hanqing__tuqiong",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable{
  ["hanqing__tuqiong"] = "图穷",
  [":hanqing__tuqiong"] = "转换技，当你使用一张牌后，你可以：阳：摸一张牌；阴：弃置一张牌，然后明置一张手牌。你不能使用或打出因此明置的手牌。",

  [":hanqing__tuqiong_yang"] = "转换技，当你使用一张牌后，你可以："..
  "<font color=\"#E0DB2F\">阳：摸一张牌；</font>阴：弃置一张牌，然后明置一张手牌。"..
  "你不能使用或打出因此明置的手牌。",
  [":hanqing__tuqiong_yin"] = "转换技，当你使用一张牌后，你可以："..
  "阳：摸一张牌；<font color=\"#E0DB2F\">阴：弃置一张牌，然后明置一张手牌。</font>"..
  "你不能使用或打出因此明置的手牌。",

  ["#hanqing__tuqiong-yang"] = "图穷：是否摸一张牌？",
  ["#hanqing__tuqiong-yin"] = "图穷：是否弃置一张牌，然后明置一张手牌？",
  ["#hanqing__tuqiong-show"] = "图穷：请明置一张手牌，你不能使用或打出以此法明置的手牌！",
  ["@@hanqing__tuqiong-inhand"] = "图穷",
}

local DIY = require "packages/diy_utility/diy_utility"

tuqiong:addEffect(fk.CardUseFinished, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(tuqiong.name) then
      if player:getSwitchSkillState(tuqiong.name, false) == fk.SwitchYang then
        return true
      else
        return not player:isNude()
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(tuqiong.name, false) == fk.SwitchYang then
      return room:askToSkillInvoke(player, {
        skill_name = tuqiong.name,
        prompt = "#hanqing__tuqiong-yang",
      })
    else
      local card = room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = tuqiong.name,
        prompt = "#hanqing__tuqiong-yin",
        cancelable = true,
        skip = true,
      })
      if #card > 0 then
        event:setCostData(self, {cards = card})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(tuqiong.name, true) == fk.SwitchYang then
      player:drawCards(1, tuqiong.name)
    else
      room:throwCard(event:getCostData(self).cards, tuqiong.name, player, player)
      if player.dead or player:isKongcheng() then return end
      local cards = table.filter(player:getCardIds("h"), function (id)
        return not table.contains(DIY.getShownCards(player), id)
      end)
      if #cards > 0 then
        local id = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = false,
          skill_name = tuqiong.name,
          pattern = tostring(Exppattern{ id = cards }),
          prompt = "#hanqing__tuqiong-show",
          cancelable = false,
        })[1]
        room:setCardMark(Fk:getCardById(id), "@@hanqing__tuqiong-inhand", 1)
        DIY.showCards(player, id)
      end
    end
  end,
})

tuqiong:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return table.find(Card:getIdList(card), function(id)
      return Fk:getCardById(id):getMark("@@hanqing__tuqiong-inhand") > 0
    end)
  end,
  prohibit_response = function(self, player, card)
    return table.find(Card:getIdList(card), function(id)
      return Fk:getCardById(id):getMark("@@hanqing__tuqiong-inhand") > 0
    end)
  end,
})

return tuqiong
